package com.lsk.qiniu;

import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.lsk.util.QiNiuConfig;
import com.lsk.util.ReturnData;
import com.lsk.util.Syslog;
import com.lsk.util.UtilUUID;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import sun.misc.BASE64Decoder;

import java.io.IOException;

import static com.lsk.util.QiNiuConfig.getuploadManger;

public class QiniuController extends Controller {
    Cache cache= Redis.use("ACTIVITYUSER");
    /**
     * key为指定图片的文件路径
     * @throws IOException
     */
    public void uploadImages() throws IOException {
        String images=getPara("images");
        String key=getPara("key");
        BASE64Decoder decoder = new BASE64Decoder();
        String imgpath="";
        try {
            byte[] bt = decoder.decodeBuffer(images);
            imgpath=QiNiuConfig.Quickdomain+picture(bt,key+ UtilUUID.getUUID());

        }catch (Exception e){
            e.printStackTrace();

        }
        renderJson(imgpath);
    }

    //    上传图片的
    public   static String picture(byte b[],String key){
        //构建一个配置类
        UploadManager uploadManager= getuploadManger();
        //获取上传的token
//        String upToken=QiNiuConfig.getClientTokenQiNiu(QiNiuConfig.QiNiuPath);
//        String upToken=cache.get("upToken");
//        if(upToken==null) {
            // 放到缓存30分钟
            String upToken=QiNiuConfig.getClientTokenQiNiu(QiNiuConfig.QiNiuPath);
//            cache.setex("upToken", 60 * 30, upToken);
//        }
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        try {
            Response response = uploadManager.put(b, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            key=putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
            }
        }
        return  key;
    }

    //获取七牛上传图片的token
    public void getToken(){
        ReturnData returnData=new ReturnData();
        String upToken=cache.get("upToken");
        if(upToken==null) {
            // 放到缓存30分钟
            upToken=QiNiuConfig.getClientTokenQiNiu(QiNiuConfig.QiNiuPath);
            cache.setex("upToken", 60 * 30, upToken);
        }
        returnData.setData(upToken);
        renderJson(returnData);
    }

    /**
     * 文件图片上传
     * @param
     */
    public static String UploadFileByFile(String localFilePath,String key,String upToken) {

        Syslog.info("当前页面路径为："+localFilePath);

        UploadManager uploadManager = getuploadManger();
        String path="";
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            path=putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            Syslog.error("error"+ex.response.error);
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                Syslog.error(ex2);
                //ignore
            }
        }
        return path;
    }



}
